package com.we823.project.common.file;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.we823.project.model.repo.CategoryModel;
import com.we823.project.model.repo.MaterialModel;
import com.we823.project.model.repo.MaterialStockModel;
import com.we823.project.model.report.StockReport;

public class MaterialStockUtil extends ExcelUtil<MaterialStockModel> {

	private String action = "year";
	
	public MaterialStockUtil() {
		this("year");
	}
	
	public MaterialStockUtil(String action) {
		this.action = action;
	}
	@Override
	public List<String[]> parseContent(List<MaterialStockModel> content) {
		if (content != null) {
			List<StockReport> stockReports = new ArrayList<>();
			int count = 0;
			if (content != null) {
				count = content.size();
			}
			for (int index = 0; index < count; index++) {
				StockReport stockReport = new StockReport();

				MaterialStockModel materialStockModel = content.get(index);
				stockReport.setId(index);
				if (materialStockModel.getMaterialModel() != null) {
					MaterialModel materialModel = materialStockModel
							.getMaterialModel();
					CategoryModel categoryModel = materialModel.getCategory();
					if (categoryModel != null) {
						stockReport.setCategory(categoryModel.getName());
					}
					if (StringUtils.isEmpty(materialModel.getColor())) {
						stockReport.setModel(materialModel.getSpecification());
					} else {
						stockReport.setModel(materialModel.getSpecification() + "("
								+ materialModel.getColor() + ")");
					}
					stockReport.setUnit(materialModel.getUnit());
				} else {
					stockReport.setCategory("未知");
				}

				stockReport.setStockDate(buildStockDate(materialStockModel));
				stockReport.setMaterialCode(materialStockModel
						.getMaterialCode());
				stockReport.setMaterialName(materialStockModel
						.getMaterialName());
				stockReport.setStock(materialStockModel.getStock());

				stockReports.add(stockReport);
			}

			List<String[]> contentList = new ArrayList<String[]>();
			for (StockReport stockReport : stockReports) {
				String[] contents = new String[] {
						stockReport.getId() + 1 + "",
						stockReport.getStockDate(), stockReport.getCategory(),
						stockReport.getMaterialCode(),
						stockReport.getMaterialName(), stockReport.getModel(),
						stockReport.getUnit(), stockReport.getStock() + "" };
				contentList.add(contents);
			}

			return contentList;
		}
		return null;
	}

	@Override
	public String[] buildHeader() {
		String[] headers = new String[] { "序号", "会计期间", "物料类别", "物料代码", "物料名称",
				"规格型号", "单位", "结存数量" };
		return headers;
	}
	
	public String buildStockDate(MaterialStockModel materialStockModel) {
		String stockDate = null;
		if("year".equals(action)) {
			stockDate = materialStockModel.getYear()+"";
		}else if("month".equals(action)) {
			stockDate = materialStockModel.getYear()+"-"+materialStockModel.getMonth();
		}else {
			stockDate = materialStockModel.getYear()+"-"+materialStockModel.getMonth()+"-"+materialStockModel.getDay();
		}
		return stockDate;
	}

}
